package com.ruoyi.supplier.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.supplier.domain.vo.purchase.PurchaseCreateVo;
import com.ruoyi.supplier.domain.vo.purchase.PurchaseDetailVo;
import com.ruoyi.supplier.domain.vo.purchase.PurchasePageQueryVo;
import com.ruoyi.supplier.manager.PurchaseManager;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.supplier.domain.vo.PurchaseVo;
import com.ruoyi.supplier.domain.bo.PurchaseBo;
import com.ruoyi.supplier.service.IPurchaseService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 采购单控制器
 * 前端访问路由地址为:/supplier/purchase
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/purchase")
public class PurchaseController extends BaseController {

    private final IPurchaseService iPurchaseService;

    @Autowired
    private PurchaseManager purchaseManager;

    /**
     * 查询采购单列表
     */
    @SaCheckPermission("supplier:purchase:list")
    @GetMapping("/list")
    public TableDataInfo<PurchaseVo> list(PurchaseBo bo, PageQuery pageQuery) {
        return iPurchaseService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出采购单列表
     */
    @SaCheckPermission("supplier:purchase:export")
    @Log(title = "采购单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PurchaseBo bo, HttpServletResponse response) {
        List<PurchaseVo> list = iPurchaseService.queryList(bo);
        ExcelUtil.exportExcel(list, "采购单", PurchaseVo.class, response);
    }

    /**
     * 获取采购单详细信息
     *
     * @param purchaseId 主键
     */
    @SaCheckPermission("supplier:purchase:query")
    @GetMapping("/{purchaseId}")
    public R<PurchaseVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer purchaseId) {
        return R.ok(iPurchaseService.queryById(purchaseId));
    }

    /**
     * 新增采购单
     */
    @SaCheckPermission("supplier:purchase:add")
    @Log(title = "采购单", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PurchaseBo bo) {
        return toAjax(iPurchaseService.insertByBo(bo));
    }

    /**
     * 修改采购单
     */
    @SaCheckPermission("supplier:purchase:edit")
    @Log(title = "采购单", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PurchaseBo bo) {
        return toAjax(iPurchaseService.updateByBo(bo));
    }

    /**
     * 删除采购单
     *
     * @param purchaseIds 主键串
     */
    @SaCheckPermission("supplier:purchase:remove")
    @Log(title = "采购单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{purchaseIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Integer[] purchaseIds) {
        return toAjax(iPurchaseService.deleteWithValidByIds(Arrays.asList(purchaseIds), true));
    }

    /**
     * 新增采购单
     */
    @PostMapping()
    public R<Void> create(@Validated @RequestBody PurchaseCreateVo createVo) {
        return toAjax(purchaseManager.create(createVo));
    }

    /**
     * 查询采购单列表
     */
    @PostMapping("/index")
    public TableDataInfo<PurchaseVo> index(PurchasePageQueryVo pageQuery) {
        return purchaseManager.queryPageList(pageQuery);
    }

    /**
     * 查询采购单列表
     */
    @GetMapping("/detail")
    public R<PurchaseDetailVo> detail(Integer purchaseId) {
        return R.ok(purchaseManager.detail(purchaseId));
    }

}
